
package com.idealighter.game.dictionary.dic;

import com.google.inject.Inject;
import com.google.inject.Singleton;

import com.idealighter.game.dictionary.core.DictionaryTable;
import com.idealighter.game.dictionary.domain.HorceraceLogTypeDomain;
import com.idealighter.game.dictionary.mapper.HorceraceLogTypeMapper;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.atomic.AtomicInteger;

@Singleton
public class HorceraceLogTypeDic implements DictionaryTable<HorceraceLogTypeDomain> {

  private HorceraceLogTypeMapper mapper;
  private List<HorceraceLogTypeDomain> list;
  private Map<Integer, HorceraceLogTypeDomain> map;
  private int totalWeigh;

  @Inject
  public HorceraceLogTypeDic(HorceraceLogTypeMapper mapper) {
    this.mapper = mapper;
    load();
  }

  @Override
  public List<HorceraceLogTypeDomain> list() {

    return list;
  }

  @Override
  public Map<Integer, HorceraceLogTypeDomain> map() {

    return map;
  }

  @Override
  public void load() {
    this.list = mapper.selectAll();
    AtomicInteger totalWeightTmp = new AtomicInteger(0);
    Map<Integer, HorceraceLogTypeDomain> map = new HashMap<>();
    list.forEach(val -> {
      map.put(val.getPlanId(), val);
      totalWeightTmp.getAndAdd(val.getPlanProbability());

    });
    this.map = map;
    this.totalWeigh = totalWeightTmp.intValue();
  }

  @Override
  public HorceraceLogTypeDomain get(int id) {

    return map.get(id);
  }

  public int getTotalWeight() {
    return totalWeigh;
  }

}
